#include <windows.h>
#include "Display_eBox2300sx.h"
#include "regmgr.h"


TCHAR *eBox2300sxResolutions[116] = {
	TEXT("640x480x16-60Hz"),
	TEXT("640x480x16-75Hz"),
	TEXT("640x480x16-85Hz"),
	TEXT("800x600x16-60Hz"),
	TEXT("800x600x16-75Hz"),
	TEXT("800x600x16-85Hz"),
	TEXT("1024x768x16-60Hz"),
	TEXT("1024x768x16-75Hz"),
	TEXT("1024x768x16-85Hz"),
	TEXT("1280x1024x16-60Hz"),
	TEXT("1280x1024x16-75Hz"),
	TEXT("1280x1024x16-85Hz"),
	TEXT("640x480x32-60Hz"),
	TEXT("640x480x32-75Hz"),
	TEXT("640x480x32-85Hz"),
	TEXT("800x600x32-60Hz"),
	TEXT("800x600x32-75Hz"),
	TEXT("800x600x32-85Hz"),
	TEXT("1024x768x32-60Hz"),
	TEXT("1024x768x32-75Hz"),
	TEXT("1024x768x32-85Hz"),
	TEXT("1280x1024x32-60Hz"),
	TEXT("1280x720x16-60Hz"),
	TEXT("1280x720x32-60Hz"),
	TEXT("1280x768x16-60Hz"),
	TEXT("1280x768x32-60Hz"),
	TEXT("1280x800x16-60Hz"),
	TEXT("1280x800x32-60Hz"),
	TEXT("1280x960x16-60Hz"),
	TEXT("1280x960x32-60Hz"),
	TEXT("1368x768x16-60Hz"),
	TEXT("1368x768x32-60Hz"),
	TEXT("1400x1050x16-60Hz"),
	TEXT("1400x1050x32-60Hz"),
	TEXT("1440x900x16-60Hz"),
	TEXT("1440x900x32-60Hz"),
	TEXT("1600x1200x16-60Hz"),
	TEXT("1600x1200x32-60Hz"),
	TEXT("1680x1050x16-60Hz"),
	TEXT("1680x1050x32-60Hz"),
	NULL
};


Display_eBox2300sx::Display_eBox2300sx()
{
}


Display_eBox2300sx::~Display_eBox2300sx()
{
}

BOOL Display_eBox2300sx::IsSupported()
{

	BOOL rez = FALSE;
	HKEY hKey;

	if ( 0 == RegOpenKeyEx(HKEY_LOCAL_MACHINE, EBOX2300SX_IS_SUPPORTED,0,0, &hKey) )
	{
		rez = TRUE;
	}

	if ( hKey != NULL )
	{
		CloseRegistryKey(hKey);
	}

	return rez;
}

BOOL Display_eBox2300sx::ModeIsSupported( DWORD width, 
			                       DWORD height,
						  		   DWORD colorDepth,
								   DWORD freq )
{
	TCHAR settings[100]={0};

	if ( colorDepth < 17 ) 
	{
		colorDepth = 16;
	}
	else 
	{
		colorDepth = 32;
	}

	_stprintf(settings, TEXT("%dx%dx%d-%dHz"),width, height,colorDepth,freq);

	
	for ( int i = 0; eBox2300sxResolutions[i] != NULL; i++ )
	{
		if ( _tcsicmp(eBox2300sxResolutions[i], settings) == 0 )
		{
			return TRUE;
		}
	}

	return FALSE;
}

BOOL Display_eBox2300sx::TestModeIsSupported()
{
	return FALSE;
}

BOOL Display_eBox2300sx::DisplayTest(DWORD width, 
			                       DWORD height,
							       DWORD colorDepth,
							       DWORD freq )
{
	return TRUE;    
}


void Display_eBox2300sx::DisplayUpdate(DWORD width, 
								DWORD height,
							    DWORD colorDepth,
							    DWORD freq )
{
	TCHAR settings[100]={0};

	if ( colorDepth < 16 ) colorDepth = 16;
	if ( colorDepth > 32 ) colorDepth = 32;
	if ( colorDepth == 24 ) 
	{
		colorDepth = 32;
	}

	_stprintf(settings, TEXT("%dx%dx%d-%dHz"),width, height,colorDepth,freq);

	for (int i = 0; eBox2300sxResolutions[i] != NULL; i++ )
	{
		if ( _tcsicmp(eBox2300sxResolutions[i], settings) == 0 )
		{
			HKEY hKey;

			if ( 0 == RegOpenKeyEx(HKEY_LOCAL_MACHINE, EBOX2300SX_DISPLAY_SETTINGS,0,0, &hKey) )
			{
				RegSetValueEx( hKey, EBOX2300SX_DISPLAY_SETTINGS_NUM,0, REG_DWORD, (BYTE*)&i, sizeof(DWORD));
			}

			if ( hKey != NULL )
			{
				CloseRegistryKey(hKey);
			}

			return;
		}
	}
}